package com.yanggeng.demo.beginnerguidedemo.activity.customview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.widget.PieChartView;

import java.util.ArrayList;

/**
 * Created by yanggeng on 2017/6/30.
 */

public class PieChartActivity extends BaseActivity {

    private PieChart pieChart;
    private Toolbar toolbar;
    private PieChartView pieChartView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pie_chart);
        setTitle(true, "饼图");
        pieChart = (PieChart) findViewById(R.id.pie_chart);
        pieChartView = (PieChartView) findViewById(R.id.my_pie_chart);

        setPieChart();

        initPieChartView();
    }

    private void setPieChart() {
        pieChart.setUsePercentValues(false); //使用百分比显示
        pieChart.setRotationAngle(0); //起始角度
        pieChart.setHighlightPerTapEnabled(true); //设置piecahrt图表点击Item高亮是否可用
        pieChart.setRotationEnabled(false); //设置是否可以手动旋转
        pieChart.animateXY(3000, 3000, Easing.EasingOption.EaseInOutCubic, Easing.EasingOption.EaseInOutCubic); //动画设置
        pieChart.setDrawEntryLabels(false); //设置是否显示图表label

        pieChart.setDrawHoleEnabled(true); //是否显示PieChard中心圆环
        pieChart.setTransparentCircleRadius(0f); //设置中心透明圆半径
        pieChart.setDrawCenterText(true);
        pieChart.setCenterTextSize(16);
        pieChart.setCenterTextColor(Color.parseColor("#2b88e5"));
        pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                pieChart.setCenterText(((PieEntry)e).getLabel());
            }

            @Override
            public void onNothingSelected() {
                pieChart.setCenterText("");
            }
        });

        pieChart.getLegend().setEnabled(false); //设置PieChart图例是否可用
        pieChart.getDescription().setEnabled(false); // 设置PieChart描述是否可用
        setPieChartData();
    }

    private void setPieChartData() {
        ArrayList<PieEntry> pieEntryList = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#2b88e5"));
        colors.add(Color.parseColor("#FF4081"));
        PieEntry pieEntry1 = new PieEntry(40, "￥100\n停车");
        PieEntry pieEntry2 = new PieEntry(60, "￥200\n洗车");
        pieEntryList.add(pieEntry1);
        pieEntryList.add(pieEntry2);
        PieDataSet pieDataSet = new PieDataSet(pieEntryList, "消费分布");
        pieDataSet.setSliceSpace(3f);
        pieDataSet.setSelectionShift(10f);
        pieDataSet.setColors(colors);
        PieData pieData = new PieData(pieDataSet);
        pieData.setDrawValues(false);
//        pieData.setDrawValues(true);            //设置是否显示数据实体(百分比，true:以下属性才有意义)
//        pieData.setValueTextColor(Color.BLUE);  //设置所有DataSet内数据实体（百分比）的文本颜色
//        pieData.setValueTextSize(12f);          //设置所有DataSet内数据实体（百分比）的文本字体大小
////        pieData.setValueTypeface(mTfLight);     //设置所有DataSet内数据实体（百分比）的文本字体样式
//        pieData.setValueFormatter(new PercentFormatter());//设置所有DataSet内数据实体（百分比）的文本字体格式
        pieChart.setData(pieData);
        pieChart.highlightValues(null);
        pieChart.invalidate();                    //将图表重绘以显示设置的属性和数据
    }
    
    private void setToolBar() {
        int paddingTop = toolbar.getPaddingTop();
        int paddingBottom = toolbar.getPaddingBottom();
        int paddingLeft = toolbar.getPaddingLeft();
        int paddingRight = toolbar.getPaddingRight();
        int statusHeight = getStatusHeight();
        int toolbarHeight = toolbar.getLayoutParams().height;

        paddingTop += statusHeight;
        toolbar.getLayoutParams().height = toolbarHeight + statusHeight;

        toolbar.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);

    }

    private int getStatusHeight() {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    private void initPieChartView() {
        ArrayList<com.yanggeng.demo.beginnerguidedemo.bean.PieEntry> pieEntryList = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#2b88e5"));
        colors.add(Color.parseColor("#FF4081"));
        pieEntryList.add(new com.yanggeng.demo.beginnerguidedemo.bean.PieEntry(40, "￥100\n停车"));
        pieEntryList.add(new com.yanggeng.demo.beginnerguidedemo.bean.PieEntry(60, "￥200\n洗车"));
        pieChartView.setColors(colors);
        pieChartView.setData(pieEntryList);
    }
}
